package cn.gson.crm.model.service;

import java.util.List;

import cn.gson.crm.model.mapper.FunTypeMapper;
import cn.gson.crm.model.pojos.FunctionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FunTypeService {
	@Autowired
	FunTypeMapper funMapper;
	//查询一级
	public List<FunctionType> parentType(){
		return funMapper.allFunctionType(null);
	}
	//查询子级
	public List<FunctionType> functionTypes(FunctionType parentType){
		//根据父级查询子级
		List<FunctionType> functionTypes = funMapper.allFunctionType(parentType.getCategoryId());
		//循环当前级别
		for (FunctionType functionType : functionTypes) {
			//递归调用查询子级的方法，查询该级别下的子级，并赋值到该级别下的子级集合
			functionType.setFunctionTypes(this.functionTypes(functionType));
		}
		return functionTypes;
	}
	
	//查询所有类别
	public List<FunctionType> allFunctionType(){
		//查询一级
		List<FunctionType> parentType = this.parentType();
		//查询一级下的二级，在这个方法里，会递归查询子级
		for (FunctionType functionType : parentType) {
			functionType.setFunctionTypes(this.functionTypes(functionType));
		}
		return parentType;
	}
}
